package com.leyou.item.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.upload.Upload;
import com.leyou.item.dao.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Category;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author:yanjizhw@163.com
 * @date:2019-09-25 14:22
 * 描述:
 * @since:1.0.0
 */
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageInfo<Brand> queryBrand(String key, Integer page, int pageSize){

        //分页
        PageHelper.startPage(page, pageSize);
        //过滤
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)){

            example.createCriteria().orLike("name", "%" + key + "%")
                                    .orEqualTo("letter", key);
        }

        //查询
        List<Brand> brands = brandMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        //解析分页结果

        PageInfo<Brand> info = new PageInfo<Brand>(brands);

        return info;
    }

    @Transactional
    public void addBrand(Brand brand, Integer categoryId, MultipartFile image) {


        // 1、文件上传
        String file = Upload.fileUpload(image);

        // 2、品牌添加
        brand.setId(null);
        brand.setImage(file);
        int count = brandMapper.insert(brand);
        if (count != 1){
            throw new LyException(ExceptionEnum.BRAND_CREATE_FAILED);
        }

        // 3、新增中间表
        count = brandMapper.insertCategoryBrand(categoryId, brand.getId());
        if (count != 1){
            throw new LyException(ExceptionEnum.BRAND_CREATE_FAILED);
        }
    }

    public Integer queryCategoryIdByBrandId(Brand brand) {

        List<Category> categories = brandMapper.selectCategoryIdByBrandId(brand.getId());

        return categories.size() > 0 ? categories.get(0).getId() : null;
    }

    @Transactional
    public void modifyBrand(Brand brand, Integer categoryId, MultipartFile image) {

        // 1、文件上传
        String file = Upload.fileUpload(image);

        // 根据品牌id修改
        brand.setImage(file);
        int update = brandMapper.updateByPrimaryKeySelective(brand);

        if (update != 1){
            throw new LyException(ExceptionEnum.UPDATE_BRAND_FAILED);
        }

        // 根据品牌id修改商品id
        brandMapper.updateMiddleCidByBid(categoryId, brand.getId());
    }

    @Transactional
    public void moveBrand(Brand brand) {

        int delete = brandMapper.deleteByPrimaryKey(brand);

        if (delete != 1){
            throw new LyException(ExceptionEnum.DELETE_BRAND_EXCEPTION);
        }

        // 根据品牌id删除中间表
        int middle = brandMapper.deleteMiddleByBid(brand.getId());

        if (middle != 1){
            throw new LyException(ExceptionEnum.DELETE_BRAND_EXCEPTION);
        }
    }
}
